using System.Collections;
using System.Collections.Generic;
using Holoville.HOTween;
using UnityEngine;

public class levelUpInMenu : MonoBehaviour
{
	public static levelUpInMenu instance;

	public GameObject notification;

	public GameObject showLevelAfterTutor;

	public UILabel dollarTutorial;

	public UILabel goldTutorial;

	public UILabel levelTutorial;

	public UILabel dollar;

	public UILabel gold;

	public UILabel level;

	public UILabel NewContent1;

	public UILabel NewContent2;

	public WeaponBox itemPrefab;

	public GameObject WeaponsBoxes;

	public bool needActiveNotification;

	public float animTime = 2f;

	private float _itemWeight = 180f;

	private List<Product> _openedProducts = new List<Product>();

	private List<WeaponBox> _weapons = new List<WeaponBox>();

	private int _currentLevel;

	private int _tmp;

	private void Awake()
	{
		instance = this;
	}

	private void OnDestroy()
	{
		instance = null;
	}

	private void Start()
	{
		HOTween.Init();
		_currentLevel = 0;
		if (PlayerPrefs.GetInt(settings.keyLevelUp, 0) > 0)
		{
			StartCoroutine(CheckIsMissionWindowActive());
		}
		NewContent1.text = string.Empty;
		NewContent2.text = string.Empty;
		foreach (WeaponBox weapon in _weapons)
		{
			Object.Destroy(weapon);
		}
		_weapons.Clear();
		if (_currentLevel < 2)
		{
			return;
		}
		int num = 0;
		int num2 = 0;
		if (_currentLevel - 2 != num)
		{
			NewContent1.text = "Available Content";
			NewContent2.text = "Available Content";
		}
		else
		{
			NewContent1.text = "New content";
			NewContent2.text = "New content";
		}
		foreach (Product value in MarketController.Instance.allProducts.Values)
		{
			if (value.tier == num2)
			{
				_openedProducts.Add(value);
			}
		}
		for (int i = 0; i < _openedProducts.Count; i++)
		{
			if (_openedProducts[i].Price == 0)
			{
				continue;
			}
			Product product = _openedProducts[i];
			if (product != null && !product.Purchased && (product.prevProduct == null || product.prevProduct.Purchased) && product.availableForBuy)
			{
				if (_currentLevel - 2 == num)
				{
					CreateWeapon(i, product);
				}
				else
				{
					CreateWeapon(i, product);
				}
			}
		}
	}

	private void CreateWeapon(int index, Product item)
	{
		WeaponBox weaponBox = Object.Instantiate(itemPrefab);
		weaponBox.gameObject.transform.parent = WeaponsBoxes.transform;
		weaponBox.gameObject.transform.localScale = Vector3.one;
		weaponBox.gameObject.transform.localPosition = new Vector3((float)_weapons.Count * _itemWeight, 0f, 0f);
		weaponBox.gameObject.SetActive(true);
		weaponBox.productID = item.id;
		weaponBox.DrawTexture(item);
		_weapons.Add(weaponBox);
	}

	public void CloseNotification()
	{
		PlayerPrefs.Save();
		notification.SetActive(false);
		controllerMenu.thisScript.ShowInfoAfterTutorial();
	}

	public void CloseWindowWithLevelUp()
	{
		PlayerPrefs.Save();
		showLevelAfterTutor.SetActive(false);
		controllerMenu.thisScript.ShowInfoAfterTutorial();
	}

	private IEnumerator CheckIsMissionWindowActive()
	{
		yield return new WaitForEndOfFrame();
		while ((!(X3AndBonusInMenu.instance == null) && X3AndBonusInMenu.instance.x3Event.activeSelf) || (!(ManagerHUDMissionLobby.instance == null) && (ManagerHUDMissionLobby.instance.winCompleteMission.gameObject.activeSelf || ManagerHUDMissionLobby.instance.winNewMission.gameObject.activeSelf)) || controllerMenu.thisScript.blockPanel.gameObject.activeSelf || MarketControllerUI.instance.MarketActive())
		{
			yield return new WaitForEndOfFrame();
		}
		ShowNotification();
	}

	private void ShowNotification()
	{
		if (!Load.LoadBool(settings.keyShowLevelAfterTutorial))
		{
			level.text = (_currentLevel - PlayerPrefs.GetInt(settings.keyLevelUp)).ToString();
			for (int i = 1; i <= PlayerPrefs.GetInt(settings.keyLevelUp); i++)
			{
				_tmp = _currentLevel - PlayerPrefs.GetInt(settings.keyLevelUp) + i;
				StartCoroutine(Wait(_tmp, animTime * (float)i - 1f));
			}
			notification.SetActive(true);
			dollar.text = PlayerPrefs.GetInt(settings.keyObtainedDollars, 0).ToString();
			gold.text = PlayerPrefs.GetInt(settings.keyObtainedGold, 0).ToString();
		}
		else
		{
			levelTutorial.text = _currentLevel.ToString();
			dollarTutorial.text = PlayerPrefs.GetInt(settings.keyObtainedDollars, 0).ToString();
			goldTutorial.text = PlayerPrefs.GetInt(settings.keyObtainedGold, 0).ToString();
			showLevelAfterTutor.SetActive(true);
			Save.SaveBool(settings.keyShowLevelAfterTutorial, false);
		}
		PlayerPrefs.SetInt(settings.keyLevelUp, 0);
		PlayerPrefs.SetInt(settings.keyObtainedGold, 0);
		PlayerPrefs.SetInt(settings.keyObtainedDollars, 0);
		PlayerPrefs.Save();
	}

	private IEnumerator Wait(int lvl, float time)
	{
		yield return new WaitForSeconds(time);
		HOTween.To(level.gameObject.transform, animTime / 2f, new TweenParms().Prop("localScale", new Vector3(2f, 2f, 2f)).OnComplete(ReverseAnim));
		_tmp = lvl;
	}

	public void ReverseAnim()
	{
		level.text = _tmp.ToString();
		HOTween.To(level.gameObject.transform, animTime / 2f, new TweenParms().Prop("localScale", new Vector3(1f, 1f, 1f)));
	}
}
